<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/MarketShareFunction/DialogShareFunction.js"></script>
		<script type="text/javascript" src="../../../JS/addRowShareFunction.js"></script>
	</head>

	<body style="padding:0px;background-color: white">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
		<form id="ff" action="#" method="post">
			<table id="basicInfo" cellpadding="5" class="displaytable" style="text-align:left;width: 80%;margin: 0 auto;">
				<tr>
					<td width="10%">
						<h id="star">* </h>事件编号</td>
					<td width="20%">
						<input class="easyui-textbox" style="width:80%;" type="text" name="ORDER_NO" id="input_ORDER_NO" data-options="prompt:'由系统自动生成，不需填写',disabled:true" />
					</td>
					<td width="10%">
						<h id="star">* </h>发生时间</td>
					<td width="20%">
						<input class="easyui-datebox" style="width:80%" type="text" name="TIME" id="input_TIME" data-options="required:true" />
					</td>
					<td width="10%">
						<h id="star">* </h>抽检机构</td>
					<td width="20%">
						<input class="easyui-textbox" style="width: 80%;" type="text" name="INSPECTION" id="input_INSPECTION" />
					</td>

				</tr>
				<tr>
					<td width="10%">
						<h id="star">* </h>经办人</td>
					<td width="20%">
						<input class="easyui-textbox" style="width:80%;" type="text" name="OPERATOR" id="input_OPERATOR" />
					</td>
					<td width="10%">
						<h id="star">* </h>产品名称</td>
					<td width="20%">
						<input class="easyui-textbox" style="width: 80%;" type="text" name="PRODUCT" id="input_PRODUCT" data-options="required:true" />
					</td>
					<td width="10%">
						产品种类</td>
					<td width="20%">
						<input class="easyui-textbox" style="width: 80%;" type="text" name="CATEGORY" id="input_CATEGORY" data-options="required:true" />
					</td>
				</tr>
				<tr>
					<td width="10%">
						产地</td>
					<td width="20%">
						<input class="easyui-textbox" style="width: 80%;" type="text" name="ORIGIN" id="input_ORIGIN" />
					</td>
					<td width="10%">
						进货日期</td>
					<td width="20%">
						<input class="easyui-datebox" style="width:80%;" type="text" name="PURCHASEDATE" id="input_PURCHASEDATE" />
					</td>

					<td width="10%">
						抽检时间</td>
					<td width="20%">
						<input class="easyui-datebox" style="width:80%;" type="text" name="INSPECT_TIME" id="input_INSPECT_TIME" />
					</td>
				</tr>
				<tr>
					<td width="10%">
						抽检人员</td>
					<td width="20%">
						<input class="easyui-textbox" style="width: 80%;" type="text" name="INSPECTOR" id="input_INSPECTOR" />
					</td>
					<td width="10%">
						经销商</td>
					<td width="20%">
						<input class="easyui-textbox" style="width:80%;" type="text" name="DISTRIBUTOR" id="input_DISTRIBUTOR" />
					</td>
				</tr>
				<tr>
					<td width="10%">
						<h id="star">* </h>退出原因</td>
					<td width="40%" colspan="5">
						<input class="easyui-textbox" maxlength="5" style="width:95%;" type="text" name="REASON" id="input_REASON" data-options="required:true,validType:'length[0,500]'" />
					</td>
				</tr>
				<tr>
					<td width="10%">
						<h id="star">* </h>处理结果</td>
					<td width="40%" colspan="5">
						<input class="easyui-textbox" style="width:95%;" type="text" name="PROCEESED" id="input_PROCEESED" data-options="required:true,validType:'length[0,500]'" />
					</td>
				</tr>
			</table>
			<table>
				<tr style="display: none;">
					<td>
						<h id="star">* </h>ID</td>
					<td>
						<input class="easyui-textbox" type="text" name="ID" id="input_ID" />
					</td>
				</tr>
			</table>
			<table id="tt">

			</table>
			<div style="margin-left: 20%;" id="new_footer">

			</div>
		</form>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
		<script type="text/javascript" src="../../../JS/MarketShareFunction/MarketShareFunction.js"></script>
	</body>
	<script type="text/javascript">
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		var currPage; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var isVerify = 0;
		var updateData;
		var currRow;
		var _detailToolbarData;
		_detailToolbarData = AddDetailToolBar(7);
		GetDetailTable();
		if(id == undefined) {
			currid = AddFlag;
			$('#tt').edatagrid("iniRow");
		}
		$(document).ready(function(){
		if(id != AddFlag && id != undefined) {
			currid = id;
			loadRemote();
		}
		});
		var receipts_type = RECEIPT_FRONT_SYMBOL;
		receipts_type += RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_ADD_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_DELETE_SYMBOL;
		receipts_type += RECEIPT_VERIFY_SYMBOL;
		receipts_type += RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_UNVERIFY_SYMBOL;
		receipts_type += RECEIPT_EXPORT_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);
		addFooter($("#new_footer"));
		getPageMaxAndMin();

		$('#tt').datagrid('hideColumn', 'ID');
		$('#tt').datagrid('hideColumn', 'ORDER_ID');
				$('#tt').datagrid('hideColumn', 'TENANT_ID');
		$('#tt').datagrid('hideColumn', 'BOOST_ID');

		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/market/delist/getmaxminid.do";
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function loadRemote() {
			var _url = RemoteLinkAddress + '/market/delist/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result === null) {
						$.messager.alert("消息", "没有数据了!");
						return;
					}
					updateData = deepCopy(result);
					isVerify = result.ISVERIFY;
					verifyStatus(result.ISVERIFY, true);
					getFormData(result);
					currPage = result.ID;
					currid = result.ID;
					$('#tt').edatagrid('loadData', result.rows);
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		//导出
		function exportExcel() {
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						data: {
							"ID": currid
						},
						url: RemoteLinkAddress + "/market/delist/excelorder.do",
						dataType: "json", //服务器返回的数据类型
						success: function(result) {
							window.location.href = result.path;
							//							this.onClick(location = result.path);
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}

		//单据的相关功能
		function frontpage() { //上一张
			if(currPage > MinPage) {
				var _url = RemoteLinkAddress + '/market/delist/pre.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() { //下一张
			if(currPage < MaxPage) {
				var _url = RemoteLinkAddress + '/market/delist/next.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert(" 提示", "没有下张");
			}
		}

		function newdata() {
			$("#notreview").empty(); //去掉审核或者未审核的图片
			isVerify = 0; //将状态改为未审核
			currid = AddFlag;
			currPage = 0;
			$('#ff').form('reset');
			$('#tt').datagrid('loadData', {
				total: 0,
				rows: []
			});
		}

		function save() { //保存
			if(currid != AddFlag && isVerify == "1") {
				$.messager.alert('警告', '该单据已通过审核，不可修改！');
				return;
			}
			var isValid = $("#ff").form('validate');
			if(!isValid) {
				$.messager.alert("警告", "有必填项没有填写");
				return;
			}
			var s = $('#tt').edatagrid('getRows');
			if(s.length == 0) {
				$.messager.alert("警告", "明细不能为空");
				return;
			}
			if(currid != AddFlag) {
				getUpdateData();
			} else {
				getSaveData();
			}
		}

		function isTrueData() {
			var tip = true;
			var _value = "";
			var _name = "";
			$("#basicInfo input[name]").each(function(index) {
				_value = $(this).val().trim();
				_name = $(this).attr("name");
				if(_value.length == 0 && _name != "ORDER_NO") {
					tip = false;
					return;
				}
			});
			return tip;
		}

		function getUpdateData() {
			var data = submitFormData();
			data.rows = updateData.rows;
			submitData(data);
		}

		function getSaveData() {
			var data = submitFormData();
			data.rows = $("#tt").datagrid("getRows");
			submitData(data);
		}

		function submitData(_data) {
			var _url = "";
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/market/delist/save.do";
			} else {
				_url = RemoteLinkAddress + "/market/delist/update.do";
			}
			var _senddata = JSON.stringify(_data);
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					var revData = result.msg.split(";");
					if(revData[0] != "success" || result == null) {
						$.messager.alert('提示信息', '保存失败!');
						return;
					}
					if(currid == AddFlag) {
						$("#input_ORDER_NO").textbox("setValue", revData[2]);
						$("#input_ID").textbox("setValue", revData[1]);
						currid = revData[1];
						currPage = revData[1];
						$(document).ready(function(){
						loadRemote();
						});
						$.messager.alert("提示", "新建保存成功");
						return;
					}
					$.messager.alert("提示", "修改保存成功");
				},
				error: function(result) {
					$.messager.alert("错误", "保存失败");
				}
			});
		}

		function deletedata() { //删除
			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不能删除！");
				return;
			}
			if(isVerify == 1) {
				$.messager.alert('警示', "该单据已审核，不可以删除！");
				return;
			}
			$.messager.confirm('Confirm', '是否删除', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/market/delist/delete.do", {
							"ID": currid
						},
						function(result) {
							if(result.msg == "success") {
								$('#tt').datagrid('reload');
								$.messager.alert({
									title: '成功',
									msg: '删除成功'
								});
								nextpage();
							} else {
								$.messager.alert({
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function verifydata() { //审核
			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不可审核！");
				return;
			}
			if(isVerify == 1) {
				$.messager.alert('警示', "该单据已审核！");
				return;
			}
			var _url = RemoteLinkAddress + "/market/delist/verify.do";
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: {
					ID: currid
				},
				type: "POST",
				success: function(result) {
					$.messager.alert("成功", "审核成功");
					isVerify = "1";
					verifyStatus(isVerify, true);
				},
				error: function(result) {
					$.messager.alert("失败", "审核失败");
				}
			});
		}

		function unverifydata() {
			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不可审核！");
				return;
			}
			if(isVerify == 0) {
				$.messager.alert("警告", "订单已经弃审");
				return;
			}
			$.messager.confirm('Confirm', '是否弃审', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/market/delist/unverify.do", {
							"ID": currid,
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({
									title: '成功',
									msg: '弃审成功!'
								});
								isVerify = 0;
								verifyStatus(isVerify, true);
							} else {
								$.messager.alert({
									title: '错误',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function searchmore() { //查询
			AddTabIn("退市事件查询", parent.$('#tabs'), "UI/Market/DelistEventManagement/DelistEventManagementSearch.html");
		}

		function print() { //打印
			var pLocation = "";

			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不可打印！");
				return;
			}
			if(currid == AddFlag || isVerify != 1) {
				$.messager.alert('警示', "该单据未审核，不可以打印！");
				return;
			}
			pLocation = "DelistEventManagementPrint.html?id=" + currid;
			window.open(pLocation);
		}

		function GetDetailTable() {
			$('#tt').edatagrid({
				height: 290,
				toolbar: _detailToolbarData,
				collapsible: 'true', //是否折叠：是
				singleSelect: 'true', //是否单选：是
				rownumbers: 'true',
				fitColumns: 'true', //自动调整列：是
				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center'
					}, {
						field: 'ORDER_ID',
						title: '事件ID',
						width: '0%',
						align: 'center'
					}, {
						field: 'BOOST_ID',
						title: '市场ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
						}
					}, {
						field: 'TENANT_ID',
						title: 'ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "TENANT_ID",
								textField: "TENANT_ID"
							}
						}
					}, {
						field: 'NAME',
						title: '姓名',
						width: '20%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "NAME",
								textField: "NAME",
								panelHeight: "auto",
								editable: true,
								//								url: RemoteLinkAddress + '/market/tenant/listall.do',
								onChange: function(newValue, oldValue) {
									if(newValue != "" && newValue != undefined) {
										var url = RemoteLinkAddress + '/market/tenant/findbypinyin.do?TYPE=0&PINYINFIELDNAME=' + newValue.trim();
										$(this).combobox("reload", url);
									}
								},
								onSelect: function(record) {
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'TENANT_ID'
									}).target;
									target.textbox('clear');
									target.textbox('setValue', record.ID);
								}
							}
						}
					}, {
						field: 'REGION_TYPE',
						title: '区域类型',
						width: '19%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								url: RemoteLinkAddress + "/market/regiontype/listall.do",
								textField: "NAME",
								valueField: "NAME",
								editable: false,
								required: true,
								onSelect: function(record) {

									var _url = RemoteLinkAddress + "/market/region/listall.do?TYPE_ID=" + record.ID;
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'REGION_NAME'
									}).target;
									target.combobox('clear');
									target.combobox('reload', _url);
								}
							}
						}
					}, {
						field: 'REGION_NAME',
						title: '区域名称',
						width: '19%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "NAME",
								textField: "NAME",
								editable: false,
								panelHeight: "auto",
								required: true,
								mode: 'remote',
								method: 'get',
								onSelect: function(record) {
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'REGION_TYPE'
									}).target;
									var curr_Type = target.combobox('getText');
									var _url = RemoteLinkAddress + "/market/boost/listall.do?REGION_TYPE=" + curr_Type + "&REGION_NAME=" + record.NAME;
									target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'HOUSE_NUMBER'
									}).target;
									target.combobox('clear');
									target.combobox('reload', _url);
								}
							}
						}
					}, {
						field: 'HOUSE_NUMBER',
						title: '摊位门牌号',
						width: '19%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "HOUSE_NUMBER",
								textField: "HOUSE_NUMBER",
								editable: true,
								required: true,
								panelHeight: "auto",
								onSelect: function(record) {
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'BOOST_ID'
									}).target;
									target.textbox('clear');
									target.textbox('setValue', record.ID);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'REGION_NAME'
									}).target;
									target.textbox('clear');
									target.textbox('setValue', record.REGION_NAME);
									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'REGION_TYPE'
									}).target;
									target.textbox('clear');
									target.textbox('setValue', record.REGION_TYPE);
								}
							}
						}
					}, {
						field: 'QUANTITY',
						title: '进货数量',
						width: '19%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								valueField: "QUANTITY",
								textField: "QUANTITY",
								editable: true
							}
						}
					}]
				],
				onAdd: function(index, row) {
					currRow = row;
				},
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onEndEdit: function(index, row, changes) {
					editDataGridRefresh("end", row, updateData, row.isNewRecord);
				},
				onDestroy: function(index, row) {
					editDataGridRefresh("destory", row, updateData, false);
				}
			});
		}
	</script>

</html>